import * as React from 'react';
const {
  Component,
  PropTypes,
} = React;
import {findDOMNode} from 'react-dom';

export default class Iframe extends Component {
  static propTypes = {
    width: PropTypes.string,
    height: PropTypes.string,
  };

  static defaultProps = {
    width: '100%',
    height: '100%',
  };

  componentWillMount() {
    this.handleLoad = this.handleLoad.bind(this);
  }

  handleLoad() {
    try {
      const dom = findDOMNode(this);
      dom.style.height =  dom.contentWindow.document.body.scrollHeight + 'px';
    } catch (e) {
      // I know
    }
  }

  render() {
    const {
      width,
      height
    } = this.props;
    return (
      <iframe onLoad={this.handleLoad} width={width} height={height} frameBorder="0" {...this.props}></iframe>
    );
  }
}
